package net.xdclass.user.controller;


import com.qiniu.util.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.common.enums.BizCodeEnum;
import net.xdclass.user.model.vo.AddressVO;
import net.xdclass.user.request.AddressAddRequest;
import net.xdclass.user.service.AddressService;
import net.xdclass.common.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-01-26
 */

@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("根据id查询收货地址")
    @GetMapping("/detail/{id}")
    public JsonData detail(@ApiParam(value = "收货地址id", required = true)
                                @PathVariable("id") Long id) {
        AddressVO address = addressService.selectAddressById(id);
        if (address == null){
            return JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS);
        }
        return JsonData.buildSuccess(address);
    }

    /**
     * 新增收货地址
     */
    @ApiOperation("新增收货地址")
    @PostMapping("/address")
    public JsonData addAddress(@RequestBody AddressAddRequest address){
        int line = addressService.addAddress(address);
        return JsonData.buildSuccess(line);
    }

    @ApiOperation("删除指定收货地址")
    @DeleteMapping("/{id}")
    public JsonData deleteAddress(@ApiParam("收货地址id") @PathVariable("id") Long id){
        int line = addressService.deleteAddressById(id);
        return JsonData.buildSuccess(line);
    }

    @ApiOperation("查询当前用户全部收货地址")
    @GetMapping("/all")
    public JsonData selectUserAllAddress(){
        List<AddressVO> addresss = addressService.selectUserAllAddress();
        return JsonData.buildSuccess(addresss);
    }

}

